#include "../HComponent.h"
#include "../../include/HGlobalVariable.h"
#include "../../include/StdAfx.h"

HLineEdit::HLineEdit(QWidget *parent)
    :QLineEdit(parent)
{

}

HLineEdit::~HLineEdit()
{

}

void HLineEdit::focusInEvent(QFocusEvent *event)
{
    emit sigFocusStateChanged(true);
    QLineEdit::focusInEvent(event);
}

void HLineEdit::focusOutEvent(QFocusEvent *event)
{
    emit sigFocusStateChanged(false);
    QLineEdit::focusOutEvent(event);
}


HComboBox::HComboBox(QWidget *parent)
    :QComboBox(parent)
{

}

HComboBox::~HComboBox()
{

}

void HComboBox::focusInEvent(QFocusEvent *event)
{
    emit sigFocusStateChanged(true);
    QComboBox::focusInEvent(event);
}

void HComboBox::focusOutEvent(QFocusEvent *event)
{
    emit sigFocusStateChanged(false);
    QComboBox::focusOutEvent(event);
}


HNoFocusFrameDelegate::HNoFocusFrameDelegate(QObject *parent)
    :QStyledItemDelegate(parent)
{

}

HNoFocusFrameDelegate::~HNoFocusFrameDelegate()
{

}

void HNoFocusFrameDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
    QStyleOptionViewItem itemOption(option);
    // remove the focus state
    if (itemOption.state & QStyle::State_HasFocus)
    {
        itemOption.state ^= QStyle::State_HasFocus;
    }
    QStyledItemDelegate::paint(painter, itemOption, index);
}


HFlipSideWidget::HFlipSideWidget(QGraphicsItem *parent)
    :QGraphicsProxyWidget(parent)
{

}

HFlipSideWidget::~HFlipSideWidget()
{

}

QWidget* HFlipSideWidget::getCurrentContent()
{
    return this->widget();
}

void HFlipSideWidget::setCurrentContent(QWidget *currentContent)
{
    setWidget(currentContent);
    emit sendCurrentContentChange();
}

void HFlipSideWidget::mouseMoveEvent(QGraphicsSceneMouseEvent *event)
{
    emit sendButtonAreaMoved();
    QGraphicsProxyWidget::mouseMoveEvent(event);
}

HFlipableWidget::HFlipableWidget(QWidget *parent)
    :QDeclarativeView(parent)
{
    m_pwndFront = NULL;
    m_pwndBack = NULL;
    m_bMousePressed = false;
    m_bFirstMove = true;
    m_ptPressed = QPoint(0,0);

    setWindowFlags(Qt::FramelessWindowHint);
    setAttribute(Qt::WA_TranslucentBackground);
    setStyleSheet("background-color:#11ffff11");
    qmlRegisterType<HFlipSideWidget>("CFlipSideWidget", 1, 0, "FlipSideWidget");
    setSource(QUrl::fromLocalFile(QApplication::applicationDirPath() + QMLRELATIONPATH + "HFlipable.qml"));
    //setResizeMode(QDeclarativeView::SizeViewToRootObject);
    m_pRootQML = static_cast<QObject *>(this->rootObject());
    connect(m_pRootQML,SIGNAL(sigQMLButtonAreaMoved()),this,SLOT(sltButtonAreaMoved()));
}

HFlipableWidget::~HFlipableWidget()
{

}

void HFlipableWidget::setFrontWidget(QWidget *wndFront)
{
    m_pwndFront = wndFront;
    QVariant v = QVariant::fromValue((QWidget*)wndFront);
    bool bRet = QMetaObject::invokeMethod(m_pRootQML, "setfrontWidget", Q_ARG(QVariant, v));
    this->setFixedSize(calculateContainer(wndFront->size()));
}

void HFlipableWidget::setBackWidget(QWidget *wndBack)
{
    m_pwndBack = wndBack;
    QVariant v = QVariant::fromValue((QWidget*)wndBack);
    bool bRet = QMetaObject::invokeMethod(m_pRootQML, "setbackWidget", Q_ARG(QVariant, v));
    this->setFixedSize(calculateContainer(wndBack->size()));
}

void HFlipableWidget::mousePressEvent(QMouseEvent *event)
{
    m_bMousePressed = true;
    m_bFirstMove = true;
    m_ptPressed = event->pos();
    QDeclarativeView::mousePressEvent(event);
}

void HFlipableWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(m_bMousePressed)
    {
       if(m_bFirstMove)
       {
           m_bFirstMove = false;
       }
       else
       {
           int dx = event->pos().x() - m_ptPressed.x();
           int dy = event->pos().y() - m_ptPressed.y();
           this->move(this->x() + dx,this->y() + dy);
       }
    }
    QDeclarativeView::mouseMoveEvent(event);
}

void HFlipableWidget::mouseReleaseEvent(QMouseEvent *event)
{
    m_bMousePressed = false;
    QDeclarativeView::mouseReleaseEvent(event);
}

QSize HFlipableWidget::calculateContainer(QSize nContentSize)
{
    QSize resultSize;
    resultSize.setHeight(nContentSize.height() * 1.1);
    resultSize.setWidth(nContentSize.width());
    return resultSize;
}

void HFlipableWidget::sltShowFront()
{
    bool bRet = QMetaObject::invokeMethod(m_pRootQML, "showFront");
}

void HFlipableWidget::sltShowBack()
{
    bool bRet = QMetaObject::invokeMethod(m_pRootQML, "showBack");
}

void HFlipableWidget::sltButtonAreaMoved()
{
    m_bMousePressed = false;
}


HMoveableWidget::HMoveableWidget(QWidget *parent)
    :QWidget(parent)
{
    m_bPressed = false;
    m_posPressed = QPoint(0,0);
}

HMoveableWidget::~HMoveableWidget()
{

}

void HMoveableWidget::mousePressEvent(QMouseEvent *event)
{
    m_bPressed = true;
    m_posPressed = event->pos();
    QWidget::mousePressEvent(event);
}

void HMoveableWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(m_bPressed)
    {
        QPoint currentPos = event->pos();
        int dx = currentPos.x() - m_posPressed.x();
        int dy = currentPos.y() - m_posPressed.y();

        this->move(this->pos().x() + dx,this->pos().y() + dy);
    }
    QWidget::mouseMoveEvent(event);
}

void HMoveableWidget::mouseReleaseEvent(QMouseEvent *event)
{
    m_bPressed = false;
    QWidget::mouseReleaseEvent(event);
}

HClickableWidget::HClickableWidget(QWidget *parent /*= 0*/)
	:QWidget(parent)
{
	m_ptPressed = QPoint(0,0);
}

HClickableWidget::~HClickableWidget()
{

}

void HClickableWidget::mouseDoubleClickEvent(QMouseEvent *event)
{
	emit sigMouseDoubleClicked();
	QWidget::mouseDoubleClickEvent(event);
}

void HClickableWidget::mousePressEvent(QMouseEvent *event)
{
	m_ptPressed = event->pos();
	QWidget::mousePressEvent(event);
}

void HClickableWidget::mouseReleaseEvent(QMouseEvent *event)
{
	if(m_ptPressed == event->pos())
	{
		emit sigMouseClicked();
	}
	QWidget::mouseReleaseEvent(event);
}

void HClickableWidget::paintEvent(QPaintEvent *event)
{
	QStyleOption opt;
	opt.init(this);
	QPainter p(this);
	style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
	QWidget::paintEvent(event);
}


HBorderTwinklingWidget::HBorderTwinklingWidget(QWidget *parent)
    :QWidget(parent)
{
    m_strBackgroundColor = "#303030";
    m_strNormalColor = "#1d1d1d";
    m_strCheckedColor = "#78ff27";
    m_strTwinklingColor1 = "#ff0000";
    m_strTwinklingColor2 = "#1d1d1d";
    m_nBorderWidth = 2;

    m_bChecked = false;
    m_bTwinkling = false;
    m_bShowTwinkling1 = true;

    m_pSound = new QSound(QApplication::applicationDirPath() + WARNINGSOUNDRELATIONPATH + "warning.wav");
    m_pSound->setLoops(-1);
    m_pSound->stop();

    m_pTimer = new QTimer();
    m_pTimer->setInterval(100);
    m_pTimer->stop();    
    connect(m_pTimer,SIGNAL(timeout()),this,SLOT(sltTimerTimeOut()));
}

HBorderTwinklingWidget::~HBorderTwinklingWidget()
{

}

void HBorderTwinklingWidget::setNormalColor(QString strColor)
{
    m_strNormalColor = strColor;
}

void HBorderTwinklingWidget::setCheckedColor(QString strColor)
{
    m_strCheckedColor = strColor;
}

void HBorderTwinklingWidget::setTwinklingColor(QString strTwinklingColor1, QString strTwinklingColor2)
{
    m_strTwinklingColor1 = strTwinklingColor1;
    m_strTwinklingColor2 = strTwinklingColor2;
}

void HBorderTwinklingWidget::setBorderWidth(int nWidth)
{
    m_nBorderWidth = nWidth;
}

void HBorderTwinklingWidget::setChecked(bool bChecked)
{
    m_bChecked = bChecked;
    update(m_rectLeft);
	update(m_rectUp);
	update(m_rectRight);
	update(m_rectBottom);
}

void HBorderTwinklingWidget::startTwinkling()
{
	if(!m_bTwinkling)
	{
		m_bTwinkling = true;
		m_pTimer->start();
        m_pSound->play();
		update(m_rectLeft);
		update(m_rectUp);
		update(m_rectRight);
		update(m_rectBottom);
	}
}

void HBorderTwinklingWidget::stopTwinkling()
{
    m_bTwinkling = false;
    m_pTimer->stop();
    m_pSound->stop();
    update(m_rectLeft);
	update(m_rectUp);
	update(m_rectRight);
	update(m_rectBottom);
}

void HBorderTwinklingWidget::paintEvent(QPaintEvent *event)
{

	m_rectLeft = QRect(0,0,m_nBorderWidth,this->height());
	m_rectUp = QRect(0,0,this->width(),m_nBorderWidth);
	m_rectRight = QRect(this->width() - m_nBorderWidth,0,m_nBorderWidth,this->height());
	m_rectBottom = QRect(0, this->height() - m_nBorderWidth,this->width(),m_nBorderWidth);

	QPainter painter(this);
	painter.fillRect(this->rect(),QColor(m_strBackgroundColor));

	if(m_bTwinkling)
	{
		if(m_bShowTwinkling1)
		{
			painter.fillRect(m_rectLeft,QColor(m_strTwinklingColor1));
			painter.fillRect(m_rectUp,QColor(m_strTwinklingColor1));
			painter.fillRect(m_rectRight,QColor(m_strTwinklingColor1));
			painter.fillRect(m_rectBottom,QColor(m_strTwinklingColor1));
		}
		else
		{
			painter.fillRect(m_rectLeft,QColor(m_strTwinklingColor2));
			painter.fillRect(m_rectUp,QColor(m_strTwinklingColor2));
			painter.fillRect(m_rectRight,QColor(m_strTwinklingColor2));
			painter.fillRect(m_rectBottom,QColor(m_strTwinklingColor2));
		}
	}
	else
	{
		if(m_bChecked)
		{
			painter.fillRect(m_rectLeft,QColor(m_strCheckedColor));
			painter.fillRect(m_rectUp,QColor(m_strCheckedColor));
			painter.fillRect(m_rectRight,QColor(m_strCheckedColor));
			painter.fillRect(m_rectBottom,QColor(m_strCheckedColor));
		}
		else
		{
			painter.fillRect(m_rectLeft,QColor(m_strNormalColor));
			painter.fillRect(m_rectUp,QColor(m_strNormalColor));
			painter.fillRect(m_rectRight,QColor(m_strNormalColor));
			painter.fillRect(m_rectBottom,QColor(m_strNormalColor));
		}
	}
	QWidget::paintEvent(event);
}

void HBorderTwinklingWidget::sltTimerTimeOut()
{
    m_bShowTwinkling1 = ! m_bShowTwinkling1;
	update(m_rectLeft);
	update(m_rectUp);
	update(m_rectRight);
	update(m_rectBottom);
}
